import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        System.out.println(new Solution().partitionLabels("ababcbacadefegdehijhklij"));
        System.out.println(new Solution().partitionLabels("eccbbbbdec"));
    }
}

class Solution {
    public List<Integer> partitionLabels(String s) {

        //last occurrence of a character
        Map<Character, Integer> right = new HashMap<>();
        for (int i = s.length() - 1; i >= 0; i--) {
            if (!right.containsKey(s.charAt(i))) {
                right.put(s.charAt(i), i);
            }
        }

        List<Integer> ans = new ArrayList<>();
        int segLeft = 0, segRight = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            segRight = Math.max(right.get(c), segRight);

            if (i == segRight) {
                ans.add(segRight - segLeft + 1);
                segLeft = segRight = i + 1;
            }
        }
        return ans;
    }
}